package com.ajun.controller;


import com.ajun.entity.Specification;
import com.ajun.service.ISpecificationService;
import com.ajun.vo.SpecificationVo;
import com.example.common.util.PageResult;
import com.example.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzj
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Autowired
    private ISpecificationService specificationService;

    //规格列表
    @RequestMapping("/list")
    public R findAll(){
        List<Specification> list = specificationService.list();
        return R.ok().data("list",list);
    }
//    public R getSpecification() {
//        return R.ok().data("list",specificationService.list());
//    }


    // 分页查询
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Specification> pr = specificationService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
    // 条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) SpecificationVo vo){
        PageResult<Specification> pr = specificationService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    //添加
    @PostMapping("/add")
    public R add(@RequestBody Specification specification){
        boolean flag = specificationService.add(specification);
        return flag ? R.ok().message("添加成功"):R.error().message("添加失败");
    }

    //修改
    @PutMapping("/update")
    public R update(@RequestBody Specification specification){
        boolean flag = specificationService.updateById(specification);
        return flag ? R.ok().message("修改成功") : R.error().message("修改失败");
    }


    // 删除
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return specificationService.removeByIds(ids) ? R.ok().message("删除成功") : R.error().message("删除失败");
    }
}
